package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.voip;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * 此类用于解析拨打电话结果
 * @author alonso lee
 *
 */
public class MakeVoipCallParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private int code = 0;
	private String sessionid = "";
	
	private final String SESSION_TAG = "sessionid";
	private final String ERROR_TAG = "error";
	
	@Override
	public void parseIQPackage(ALIQ packact, String stream, ALXmppEventListener listener)
			throws Exception {
		// TODO Auto-generated method stub
		mListener = listener;
		sessionid = null;
		code = 0;
		startParse(packact, stream, this);

	}

	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
			ALLog.e("make code error: " + code);
		}
	}

	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if(tag.equals(SESSION_TAG)){
			sessionid = readText();
			ALLog.d("current session "+ sessionid);
		}
	}

	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if(mListener != null){
			mListener.onGetMakeVoipCallResult(code, sessionid);
		}
	}

}
